<?php

namespace app\admin\controller\member;

use app\admin\model\UM as UM;
use app\admin\service\LawyerExportService;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use response\Excel;
use think\APP;
use think\Collection;
use think\Request as Request;
use think\Response;

/**
 * LawyerExport 控制器
 * Class LawyerExport
 * @package app\admin\controller\member;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\admin\validate\member\LawyerExport
 */
class LawyerExport
{
    // 请求参数
    private Request $request;

    // service
    private LawyerExportService $service;

    public function __construct(Request $request, LawyerExportService $lawyerExportService)
    {
        $this->request = $request;
        $this->service = $lawyerExportService;
    }

    /**
     * 已入驻律师-导出
     * @param UM|null $user
     * @return Response
     * @url /member/lawyer_export/index
     */
    public function index(?UM $user)
    {
        $request = $this->request;

        $startTime = $request->param('startTime', null);
        $endTime = $request->param('endTime', null);

        if (is_blank($startTime)) {
            $startTime = null;
        }

        if (is_blank($endTime)) {
            $endTime = null;
        }

        $collection = $this->service->index($user, $startTime, $endTime);

        $spreadsheet = new Spreadsheet();

        // 填数据
        $this->change($spreadsheet, $collection);

        $filename = '律师入驻信息';

        return new Excel($spreadsheet, $filename);
    }

    // 填数据
    private function change(Spreadsheet $spreadsheet, Collection $collection): void
    {
        $sheet = $spreadsheet->getActiveSheet();

        //表头
        $i = 1;
        $sheet->setCellValue('A' . $i, 'UID')
            ->setCellValue('B' . $i, '昵称')
            ->setCellValue('C' . $i, '真实姓名')
            ->setCellValue('D' . $i, '城市')
            ->setCellValue('E' . $i, '律所')
            ->setCellValue('F' . $i, '电话')
            ->setCellValue('G' . $i, '入驻时间')
            ->setCellValue('H' . $i, '是否关注公众号')
            ->setCellValue('I' . $i, '余额')
            ->setCellValue('J' . $i, '获客卡余额')
            ->setCellValue('K' . $i, '通讯币余额')
            ->setCellValue('L' . $i, '累计充值金额')
            ->setCellValue('M' . $i, '累计获客卡消费数量')
            ->setCellValue('N' . $i, '累计通讯币消费数量')
            ->setCellValue('O' . $i, '注册来源');

        //  姓名，城市，律所，电话，入住时间， 是否关注公众号，（余额，获客卡余额， 通讯比余额， 累计充值金额， 累计获客卡消费数量， 累计通讯比消费数量），注册来源

        // 数据
        $collection->each(function (\app\common\model\Member $member, $key) use ($sheet, &$i) {
            $i++;
            $city = $member->city;
            $memberInfo = $member->memberInfo;
            $account = $member->account;
            $card = $member->card;
            $coin = $member->coin;

            $followedWechatStr = ['否', '是'][$member['followedWechat']] ?? '';

            $sheet->setCellValue('A' . $i, $member['id'])
                ->setCellValue('B' . $i, $member['nickname'])
                ->setCellValue('C' . $i, $member['realname'])
                ->setCellValue('D' . $i, $city ? $city['name'] : '')
                ->setCellValue('E' . $i, $memberInfo ? $memberInfo['practicingLawFirm'] : '')
                ->setCellValue('F' . $i, $member['phone'])
                ->setCellValue('G' . $i, $memberInfo ? date('Y-m-d H:i', $memberInfo['checkTime']) : '')
                ->setCellValue('H' . $i, $followedWechatStr)
                ->setCellValue('I' . $i, $account ? $account['balance'] : '')
                ->setCellValue('J' . $i, $card ? $card['balance'] : '')
                ->setCellValue('K' . $i, $coin ? $coin['balance'] : '')
                ->setCellValue('L' . $i, $member['token_recharge_trades_sum'])
                ->setCellValue('M' . $i, $member['cardTotalSum'])
                ->setCellValue('N' . $i, $member['coinTotalSum'])
                ->setCellValue('O' . $i, $member['channelStr']);
        });


    }
}
